package com.kstech.exccheck.receiver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.kstech.exccheck.R;
import com.kstech.exccheck.config.vo.ResourceVO.MsgVO;
import com.kstech.exccheck.config.vo.RealTimeParamVO;
import com.kstech.exccheck.domain.check.CheckTimer;
import com.kstech.exccheck.util.AppContext;
import com.kstech.exccheck.util.ViewTemplateUtil;
import com.kstech.exccheck.view.check.SingleCheckActivity;

/**
 * 单项检测实时参数Receiver
 * @author wanghaibin
 * @created 2016-10-20 下午10:16:35
 * @since v1.0
 */
public class SingleCheckRealTimeParamReceiver extends BroadcastReceiver implements ReceivedDataKey{
	private Map<String,SingleCheckMsgValueView> viewMap = new HashMap<String,SingleCheckMsgValueView>();
	private SingleCheckActivity activity;
	private String itemId;
	@SuppressWarnings({ "unchecked" })
	@Override
	public void onReceive(Context arg0, Intent intent) {
		if(intent.getBooleanExtra("null",false)){
			reSetView();
			return;
		}
		Map<String,String> dataMap = (Map<String,String>)intent.getSerializableExtra(REAL_PARAM_DATA);
		MsgVO msgVO = (MsgVO)intent.getSerializableExtra(ERROR_MSG);
		if( dataMap != null ){
			for(Entry<String, String> entry :dataMap.entrySet()){
				SingleCheckMsgValueView singleCheckMsgValueView = viewMap.get(entry.getKey());
				if (singleCheckMsgValueView != null ) {
					TextView valueTv = singleCheckMsgValueView.getValueTv();
					if (valueTv != null) {
						if (null == entry.getValue() || "".equals(entry.getValue())) {
							valueTv.setText(AppContext.context.getResources().getString(R.string.noRealTimeData));
						} else {
							valueTv.setText(String.valueOf(entry.getValue()));
						}
						TextView msgTv = singleCheckMsgValueView.getMsgTv();
						if (msgVO != null) {
							// 信息面板显示信息
							msgTv.setText(msgVO.getContent());
							msgTv.setVisibility(View.VISIBLE);
							valueTv.setTextColor(AppContext.context.getResources().getColor(R.color.red));
							singleCheckMsgValueView.setWorking(false);
							activity.checkUINotice("传感器故障", CheckTimer.READY_ERROR);
						} else {
							msgTv.setVisibility(View.GONE);
							valueTv.setTextColor(AppContext.context.getResources().getColor(R.color.black));
							singleCheckMsgValueView.setWorking(true);
							if(getAllRealStatus(activity.checkItemEntity.getItemId()) && "开始测量".equals(activity.singleCheckBeginCheckLeftBtn.getText().toString())){
								activity.checkUINotice("无故障，可以开始检测", CheckTimer.READY_FINISH);
							}
						}
					}
				}
			}
		}
	}

	public void initTable(TableLayout realTimeParamBody, String itemId, LinearLayout msgLayoutView, SingleCheckActivity activity) {
		this.activity = activity;
		this.itemId = itemId;
		// 首先移除所有视图，避免重叠多次
		realTimeParamBody.removeAllViews();
		// 获取一个检查项目的实时配置参数配置
		List<RealTimeParamVO> realTimeParamConfig = AppContext.getModelFile().getCheckItemVO(itemId).getRtParamList();
		// 获取表格模板
		ViewTemplateUtil vtu = new ViewTemplateUtil(AppContext.context);
		TableLayout realTimeTableLayout = vtu.getSingleRealTimeTableLayout();
		TableRow tableRow0 = (TableRow) realTimeTableLayout.getChildAt(0);
		TableRow tableRow1 = (TableRow) realTimeTableLayout.getChildAt(1);

		// 移除实时提示区的tv
		msgLayoutView.removeAllViews();

		for (int i = 0; i < realTimeParamConfig.size() && i < 6; i++) {
			TableLayout tl = null;
			if (i < 3) {
				tl = (TableLayout) tableRow0.getChildAt(i);
			} else {
				tl = (TableLayout) tableRow1.getChildAt(i - 3);
			}
			TableRow tr = (TableRow) tl.getChildAt(0);
			TextView nameTv = (TextView) tr.getChildAt(0);
			nameTv.setText(realTimeParamConfig.get(i).getName());
			TextView unitTv = (TextView) tr.getChildAt(1);
			unitTv.setText(realTimeParamConfig.get(i).getUnit());

			TableRow trV = (TableRow) tl.getChildAt(1);
			TextView valueTv = (TextView) trV.getChildAt(0);
			valueTv.setText(AppContext.context.getResources().getString(R.string.noRealTimeData));
			SingleCheckMsgValueView singleCheckMsgValueView = new SingleCheckMsgValueView();
			singleCheckMsgValueView.setValueTv(valueTv);
			TextView msgTv = new TextView(AppContext.context);
			msgTv.setTextColor(AppContext.context.getResources().getColor(R.color.black));
//			msgTv.setText("1111");
			msgTv.setVisibility(View.GONE);
			msgTv.setTextSize(20f);
			singleCheckMsgValueView.setMsgTv(msgTv);
			viewMap.put(realTimeParamConfig.get(i)
					.getName(), singleCheckMsgValueView);
			// 将msgTv动态的添加到子实时提示信息布局中
			msgLayoutView.addView(msgTv);
		}
		/*for (int i = 0; i < 16; i++) {
			TextView msgTv1 = new TextView(AppContext.context);
			msgTv1.setTextColor(AppContext.context.getResources().getColor(R.color.black));
			msgTv1.setText("1111");
			msgTv1.setVisibility(View.VISIBLE);
			msgTv1.setTextSize(20f);
			// 将msgTv动态的添加到子实时提示信息布局中
			msgLayoutView.addView(msgTv1);
		}*/

		realTimeTableLayout.removeView(tableRow0);
		realTimeTableLayout.removeView(tableRow1);

		realTimeParamBody.addView(tableRow0);
		realTimeParamBody.addView(tableRow1);
	}

	public boolean getAllRealStatus(String itemId){
		List<RealTimeParamVO> realTimeParamConfig = AppContext.getModelFile().getCheckItemVO(itemId).getRtParamList();
		for (int i = 0; i < realTimeParamConfig.size() && i < 6; i++){
			String name = realTimeParamConfig.get(i).getName();
			if(!viewMap.get(name).isWorking()){
				return false;
			}
		}
		return true;
	}
	public void reSetView(){
		List<RealTimeParamVO> realTimeParamConfig = AppContext.getModelFile().getCheckItemVO(itemId).getRtParamList();
		for (RealTimeParamVO real:realTimeParamConfig) {
			SingleCheckMsgValueView realTimeValue = viewMap.get(real.getName());
			realTimeValue.getValueTv().setText(AppContext.context.getResources().getString(R.string.noRealTimeData));
			realTimeValue.getValueTv().setTextColor(Color.BLACK);
			realTimeValue.getMsgTv().setVisibility(View.GONE);
			realTimeValue.setWorking(false);
			//activity.checkUINotice("检线连接异常", CheckTimer.READY_ERROR);
		}
	}

	public void viewMapclear(){
		viewMap.clear();
	}
}
